package com.wswy.wzcx.utils;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.text.format.DateFormat;
import com.facebook.common.util.UriUtil;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public final class StringUtils {

  private static final String[] provinces = {"京", "津", "沪", "渝", "冀", "晋", "辽", "吉", "黑",
      "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "甘", "川", "贵",
      "琼", "云", "青", "陕", "桂", "藏", "宁", "新","蒙","使","领","警","学","港","澳"};

  @SuppressLint("SimpleDateFormat")
  private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  public static String pureAddress(String name){
    if(TextUtils.isEmpty(name)){
      return name;
    }
    if(name.endsWith("省") || name.endsWith("市")){
      return name.substring(0,name.length() -1);
    }
    return name;
  }

  public static boolean isProvince(String s){
    for (String province : provinces) {
      if (TextUtils.equals(province,s)){
        return true;
      }
    }
    return false;
  }

  public static boolean isLetterOrDigit(char c){

    return (c >=40 && c<=57) || (c>=65 && c<= 90) || (c >= 97 && c <=122);
  }

  public static Uri getUriForAssets(String assetPath){
    return new Uri.Builder()
        .scheme(UriUtil.LOCAL_ASSET_SCHEME)
        .path(assetPath)
        .build();
  }

  public static int parseInt(String text, int defaultValue) {
    try {
      if (text != null) {
        return Integer.parseInt(text);
      }
    } catch (Throwable e) {
      //
    }
    return defaultValue;
  }

  public static long parseLong(String text, long defaultValue) {
    try {
      if (text != null) {
        return Long.parseLong(text);
      }
    } catch (Throwable e) {
      //
    }
    return defaultValue;
  }

  public static String hidePhone(String phone){

    if (TextUtils.isEmpty(phone)){
      return phone;
    }
    StringBuilder sb = new StringBuilder();
    int length = phone.length();
    for (int i = 0; i < length; i++) {
      if (i >2 && i < 7){
        sb.append('*');
      }else {
        sb.append(phone.charAt(i));
      }
    }
    return sb.toString();
  }


  private static final Calendar sCalendar = Calendar.getInstance();

  public static CharSequence formatTime(long time){
    long curr = System.currentTimeMillis()/1000;

    long offset = curr - time/1000;

    if (offset < 60*5){ //小于5分钟

      return "刚刚";

    }else if (offset <= 60*60){ //小于一小时

      return offset / 60 + "分钟前";

    } else if (offset <= 60*60*24){

      return offset / (60*60) + "小时前";

    } else if (offset <= 60*60*24*7  ){

      return offset / (60*60*24) + "天前";
    }else {

      sCalendar.setTimeInMillis(time);
      int ty = sCalendar.get(Calendar.YEAR);

      sCalendar.setTimeInMillis(curr*1000);
      int cy = sCalendar.get(Calendar.YEAR);

      if (ty == cy){
        return DateFormat.format("MM月dd日",time);
      }else {
        return DateFormat.format("yyyy年MM月dd日",time);
      }
    }

  }


  public static String formatMoney(float value){
    BigDecimal bigDecimal = BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP);
    return bigDecimal.toString();
  }

  public static String formatMoney(Double value){
    if (value == null){
      return "0.00";
    }
    BigDecimal bigDecimal = BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP);

    return bigDecimal.toString();
  }

  public static String formatMoney(double value){

    BigDecimal bigDecimal = BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP);

    return bigDecimal.toString();
  }

  public static String formatMoney(CharSequence value){
    return parseToMoney(value).toString();
  }

  public static @NonNull
  BigDecimal parseToMoney(CharSequence num){
    try{
      if (!TextUtils.isEmpty(num)){
        return BigDecimal.valueOf(Double.parseDouble(num.toString())).setScale(2, BigDecimal.ROUND_HALF_UP);
      }
    }catch (Throwable e){
      e.printStackTrace();
    }
    return BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
  }

  public static boolean isNum(CharSequence text){

    return  TextUtils.isDigitsOnly(text);
  }


  public static Date parseTime(String time){
    try {
      if (!TextUtils.isEmpty(time)){
        return sdf.parse(time);
      }
    }catch (Throwable e){
      e.printStackTrace();
    }
    return null;
  }

}